Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MarkFlagsMutuallyExclusiveAndRequired #1972

Conversation

baruchiro
Copy link

@baruchiro baruchiro commented Jun 6, 2023

Fixes #1216

I chose the most naive way.

Just like the flow of mutuallyExclusive, I cloned it to another flow for mutuallyExclusiveAndRequired, with only different condition, to return an error also if the set slice is empty.

Example:

❯ go run .
Error: exactly one of the flags in the group [op1 op2] must be set; none were set
Usage:
  cobra-temp [flags]

Flags:
  -h, --help         help for cobra-temp
      --op1 string   Option1 mutually exclusive and required with Option2
      --op2 string   Option2 mutually exclusive and required with Option1

exit status 1
❯ go run . --op1 foo
❯ go run . --op2 foo
❯ go run . --op1 foo --op2 foo
Error: exactly one of the flags in the group [op1 op2] must be set; [op1 op2] were all set
Usage:
  cobra-temp [flags]

Flags:
  -h, --help         help for cobra-temp
      --op1 string   Option1 mutually exclusive and required with Option2
      --op2 string   Option2 mutually exclusive and required with Option1

exit status 1

@CLAassistant
Copy link

CLAassistant commented Jun 6, 2023

CLA assistant check
All committers have signed the CLA.

@marckhouzam
Copy link
Collaborator

Thanks @baruchiro !
The issue this PR fixes has a long discussion.
Could you add in the description of this PR an example of how this new group can be used?

@baruchiro
Copy link
Author

@marckhouzam Description updated

@marckhouzam
Copy link
Collaborator

@marckhouzam Description updated

Thanks. Now that understand better what this does I think it overlaps with #1952 which allows to require one flag out of a group and then use the existing API MarkFlagsMutuallyExclusive to force mutual exclusiveness.

What do you think?

@baruchiro
Copy link
Author

I think #1952 is better, thanks!

@baruchiro baruchiro closed this Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Any best practice to imitate the "either-or" flags?
3 participants